﻿using System;
using System.Collections.Generic;
using System.IO;

namespace yacomp.helpers
{
    internal partial class helper
    {
        internal const char path_split_01 = '\\';
        internal const char path_split_02 = '/';
        internal const char path_point = '.';

        public static string to_standard_path(string path)
        {
            if (path == null) return string.Empty;
            if (path[path.Length-1]== path_split_02) path = path.TrimEnd(path_split_02);
            return path.Replace(path_split_01, path_split_02);
        }
        internal static bool standard_path_has_filename(string path)
        {
            return path[path.Length-1] != path_split_02;
        }
        internal static bool is_root_path(string path)
        {
            return string.IsNullOrEmpty(path) || (path.Length == 1 && path[0] == path_split_02);
        }
        public static string read_filename(string path)
        {
            if (string.IsNullOrEmpty(path)) return string.Empty;
            path= to_standard_path(path);
            if (!standard_path_has_filename(path)) return string.Empty;
            var ix = path.LastIndexOf(path_split_02);
            if (ix == -1) return path;
            return path.Substring(ix+1);
        }
        public static string read_pathname(string path)
        {
            if (string.IsNullOrEmpty(path)) return string.Empty;
            path = to_standard_path(path);
            if (!standard_path_has_filename(path)) return path;
            var ix = path.LastIndexOf(path_split_02);
            if (ix == -1) return string.Empty;
            return path.Substring(0, ix);
        }
        public static string read_before_pathname(string path)
        {
            if (string.IsNullOrEmpty(path)) return string.Empty;
            path = to_standard_path(path);
            if (path[path.Length -1] == path_split_02) path = path.Substring(0, path.Length -1);
            var ix = path.LastIndexOf(path_split_02);
            if (ix == -1) return string.Empty;
            return path.Substring(0, ix);
        }
        public static string read_file_extension(string path) // 文件后缀名
        {
            path = read_filename(path);
            if (!standard_path_has_filename(path)) return string.Empty;
            var ix = path.LastIndexOf(path_point);
            if (ix == -1) return string.Empty;
            return path.Substring(ix+1).ToLower();
        }
        public static string to_absolute_path(string path, string now_path) // 转绝对目录
        {
            if (string.IsNullOrEmpty(path)) return now_path;
            path = to_standard_path(path);
            if (path[0] == path_split_02) return path;

            path = now_path +path_split_02 + path;
            var path_factory = new Stack<string>();
            foreach (var name in path.Split(path_split_02))
            {
                if (string.IsNullOrEmpty(name)) continue;
                switch (name)
                {
                    case ".": continue;
                    case "..":
                        if (path_factory.Count>0) path_factory.Pop();
                        break;
                    default:
                        path_factory.Push(name);
                        break;
                }
            }
            if (path_factory.Count == 0) return string.Empty;
            path = path_factory.Pop();
            while (path_factory.Count > 0) path = path_factory.Pop()+ path_split_02 + path;
            path = path_split_02 + path;
            return path;
        }

        public static long read_file_size(string path)
        {
            using (var f = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) return f.Length;
        }
    }
}
